package printer.impl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Iterator;
import java.util.Set;

import pojo.Customer;
import printer.DataPrinter;

public class DataPrinterImpl implements DataPrinter {

	public StringBuilder print(Set<Customer> customer, String format) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

		StringBuilder sb = null;
		if ("xml".equals(format)) {
			sb = new StringBuilder();
			sb.append("<customers>\r\n");
			Iterator<Customer> it = customer.iterator();
			while (it.hasNext()) {
				Customer c = it.next();
				sb.append("  <customer id=\"").append(c.getId())
						.append("\">\r\n").append("    <name>")
						.append(c.getName()).append("</name>\r\n")
						.append("    <email>").append(c.getEmail())
						.append("</email>\r\n").append("    <sex>")
						.append(c.isSex()).append("</sex>\r\n")
						.append("    <birthday>")
						.append(dateFormat.format(c.getBirth()))
						.append("</birthday>\r\n").append("  </customer>\r\n");
			}
			sb.append("</customers>\r\n");
			return sb;
		} else if ("line".equals(format)) {
			sb = new StringBuilder();
			sb.append("-------------------------------------------------------------------\r\n");
			sb.append(String.format("%-10s%-10s%-15s%-10s%-10s", "id", "name",
					"email", "sex", "birthday"));
			sb.append("\r\n-------------------------------------------------------------------\r\n");
			for (Customer item : customer) {
				sb.append(
						String.format("%-10d%-10s%-15s%-10s%-10s%n",
								item.getId(), item.getName(), item.getEmail(),
								item.isSex(),
								dateFormat.format(item.getBirth()))).append(
						"\r\n");
			}
		}
		return sb;
	}

}
